﻿using System;
using System.Collections.Generic;
using System.Text;
using Entity;
using ECommerce.Library.DataStructure;

namespace AutoGear.BLL
{
    public class Inventory
    {
        public Inventory() { }

        public Inventory
        (
            int inventoryId,
            int productId,
            string productName,
            string itemNumber,
            double price,
            int qTY,
            double costValue
        )
        {
            _inventoryId = inventoryId;
            _productId = productId;
            _productName = productName;
            _itemNumber = itemNumber;
            _price = price;
            _qTY = qTY;
            _costValue = costValue;
        }


        private int _inventoryId;
        public int InventoryId
        {
            get { return _inventoryId; }
        }
        private int _productId;
        public int ProductId
        {
            get { return _productId; }
            set { _productId = value; }
        }
        private string _productName;
        public string ProductName
        {
            get { return _productName; }
            set { _productName = value; }
        }
        private string _itemNumber;
        public string ItemNumber
        {
            get { return _itemNumber; }
            set { _itemNumber = value; }
        }
         private double _price;
         public double Price
         {
             get { return _price; }
             set { _price = value; }
         }
        private int _qTY;
        public int QTY
        {
            get{return _qTY;}
            set{_qTY=value;}
        }

        private double _costValue;
        public double CostValue
        {
            get{return _costValue;}
            set{_costValue=value;}
        }

        public static Inventory Load(int InventoryId)
        {
            TblInventory tblInventory = TblInventory.GetTblInventoryByInventoryId(InventoryId);
            return new Inventory(tblInventory);
        }
        private Inventory(TblInventory tblInventory)
        {
            this._inventoryId = tblInventory.InventoryId;
            this._productId = tblInventory.ProductId;
            this._productName = tblInventory.ProductName;
            this._itemNumber = tblInventory.ItemNumber;
            this._price = Convert.ToDouble(tblInventory.Price);
            this._qTY = tblInventory.QTY;
            this._costValue = Convert.ToDouble(tblInventory.CostValue);
        }

        public void Update()
        {

            TblInventory tblInventory = TblInventory.GetTblInventoryByInventoryId(InventoryId);
            tblInventory.CostValue =Convert.ToDecimal(this.CostValue);
            tblInventory.QTY = this.QTY;
            //tblProductPurchaseOrder.PrintNo = this.PrintNo;
            TblInventory.UpdateTblInventory(tblInventory);

        }
        public static void DeleteInventory()
        {
            TblInventoryExtended.DeleteAllInventory();
        }
        public static void InsertInventory()
        {
            TblInventoryExtended.InsertInventory();
        }

        public static ReadWriteCustomCollection<Inventory> GetBackendListByCondition(string queryCondition, string sortBy, int firstItem, int pageSize)
        {
            ECommerce.DAL.CustomCollection<TblInventory> tblInventory = TblInventoryExtended.GetBackendListByCondition(queryCondition, sortBy, firstItem, pageSize);
            return LoadFromCustomCollection(tblInventory);
        }

        public static int GetBackendListByConditionCount(string queryCondition)
        {
            int count = TblInventoryExtended.GetBackendListByConditionCount(queryCondition);
            return count;
        }

        public static ReadWriteCustomCollection<Inventory> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblInventory> tblInventory)
        {
            ReadWriteCustomCollection<Inventory> inventory = new ReadWriteCustomCollection<Inventory>();
            for (int i = 0; i < tblInventory.Count; i++)
            {
                inventory.Add(new Inventory(tblInventory[i]));
            }
            return inventory;
        }

    }
}
